package cn.tom.service.impl;

import cn.tom.dao.UserDao;
import cn.tom.entity.XUser;
import cn.tom.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserDao userDao;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public XUser findByAccount(String s) {
        return userDao.findByAccount(s);
    }

    @Override
    public XUser findById(Integer uid) {
        return userDao.findById(uid);
    }

    @Override
    public int update(XUser xUser) {
        if(xUser.getPassword() != null && xUser.getPassword() != "")
            xUser.setPassword(passwordEncoder.encode(xUser.getPassword()));
        return userDao.update(xUser);
    }

    @Override
    public int add(XUser xUser) {
        xUser.setPassword(passwordEncoder.encode(xUser.getPassword()));
        return userDao.add(xUser);
    }
}
